package com.itheima.service.mongo.Impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        Query query = new Query(Criteria.where("toUserId")
                .is(toUserId)).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;
    }

    @Override
    public PageBeanVo findRecommedUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        int starInsex = (pageNum - 1) * pageSize;
        if (starInsex==0){
            starInsex++;
        }
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(starInsex).limit(pageSize);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query,RecommendUser.class);
        Query query2 = new Query(Criteria.where("toUserId").is(toUserId));
        long count2 = mongoTemplate.count(query2,RecommendUser.class);
        System.out.println("count"+count);
        System.out.println("count2"+count2);
        return new PageBeanVo(pageNum,pageSize,count,recommendUsers);
    }

    @Override
    public RecommendUser findByJiarenIdAndToUserId(Long jiarenId, Long toUserId) {
        Query query = new Query(Criteria.where("userId").is(jiarenId).and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }
}
